package com.z.xhelper;


import android.app.Application;
import android.content.ContentValues;
import android.content.Context;
import android.content.pm.ApplicationInfo;

import com.z.xhelper.core.utils.LogUtil;
import com.z.xhelper.core.xService;

import java.util.Arrays;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

public class XHookMain implements IXposedHookLoadPackage {
    private static boolean isHook = false;

    @Override
    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam loadPackageParam) {

        // 阻断系统应用 和 系统更新应用
        if (loadPackageParam.appInfo == null || (loadPackageParam.appInfo.flags & (ApplicationInfo.FLAG_SYSTEM |
                ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
            return;
        }

        if (!loadPackageParam.packageName.equals("com.tencent.mm")) {
            return;
        }

        if (!loadPackageParam.processName.equals("com.tencent.mm")){
            return;
        }

        XposedHelpers.findAndHookMethod(Application.class,"attach",Context.class,new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                super.afterHookedMethod(param);

                if (isHooked()) {
                    return;
                }
                Context context = (Context) param.args[0];
                ClassLoader appClassLoader = context.getClassLoader();
                xService.run(appClassLoader);
            }
        });


    }

    private static void testHook(ClassLoader classLoader) {
        Class<?> aClass = XposedHelpers.findClass(
                "com.tencent.wcdb.database.SQLiteDatabase",
                classLoader);
        LogUtil.log("aClass:" + aClass);
        try {
            XposedHelpers.findAndHookMethod(
                    aClass,
                    "insertWithOnConflict",
                    String.class,
                    String.class,
                    ContentValues.class,
                    int.class,
                    new XC_MethodHook() {
                        @Override
                        protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                            super.beforeHookedMethod(param);
                            LogUtil.log("TextMsgReceiveMsgHook成功before:参数");
                        }

                        @Override
                        protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                            super.afterHookedMethod(param);
                            LogUtil.log("TextMsgReceiveMsgHook成功:参数");
                        }
                    });

        } catch (Exception e) {
            LogUtil.log("寻找xxx.xxx.xxx报错" + Arrays.toString(e.getStackTrace()));
        }
    }

    private static synchronized boolean isHooked() {
        if (isHook) {
            return true;
        }
        isHook = true;
        return false;
    }

}
